<template>
  <div>
    <component v-bind:is="currentTabComponent" title="我的消息">
      <div slot="content" class="wrapper">
        <a-list
          v-if="isPC"
          bordered
          itemLayout="vertical"
          :dataSource="data"
          :pagination="paginationInfo"
        >
          <a-list-item
            slot="renderItem"
            slot-scope="item"
            @click="showModal(item)"
          >
            <a-list-item-meta :description="item.content">
              <span slot="title"
                >【{{ item.title }}】
                <span class="time">{{ item.create_time }}</span></span
              >
              <div slot="avatar" class="avatar">
                <span
                  :style="changeColor(item.title)"
                  class="iconfont icon"
                  :class="changeIcon(item.title)"
                  style="font-size: 1.31rem"
                ></span>
                <span class="dot-icon" v-if="item.isread === 0"></span>
              </div>
            </a-list-item-meta>
          </a-list-item>
        </a-list>
        <a-list
          v-else
          itemLayout="horizontal"
          :dataSource="data"
          :pagination="paginationInfo"
        >
          <a-list-item
            slot="renderItem"
            slot-scope="item"
            @click="showModal(item)"
          >
            <a-list-item-meta :description="item.content">
              <a-badge :dot="changeStatus(item.isread)" slot="avatar"
                ><a-avatar
                  shape="square"
                  :style="changeColor(item.title)"
                  :size="40"
                  >{{ item.title }}</a-avatar
                ></a-badge
              >
            </a-list-item-meta>
            <div slot="extra" style="margin-left: 56px">
              <span>{{ item.create_time }}</span>
            </div>
          </a-list-item>
        </a-list>
      </div>
    </component>
  </div>
</template>

<script>
import { Avatar, Badge, Modal } from "ant-design-vue";
import "@/assets/fonticon/iconfont.css";
export default {
  name: "mobile",
  components: {
    aAvatar: Avatar,
    aBadge: Badge,
    pcWrapper: () => import("@/components/contentWrapper/pcWrapper"),
    contentWrapper: () => import("@/components/contentWrapper/index")
  },
  data() {
    let that = this;
    return {
      data: [],
      paginationInfo: {
        onChange: pagination => {
          that.changePagination(pagination);
        },
        total: 0,
        current: 1,
        pageSize: 10
      }
    };
  },
  methods: {
    changePagination(pagination) {
      this.paginationInfo.current = pagination;
      this.getList();
    },
    showModal(item) {
      let that = this;
      Modal.info({
        title: item.title,
        content: item.content,
        onOk() {
          if (item.isread) {
            return false;
          } else {
            that.$ajax
              .post(that.$api.readNotice, {
                id: item.id
              })
              .then(() => {
                that.getList();
              })
              .catch(err => {
                that.$message.error(err);
              });
          }
        }
      });
    },
    changeIcon(status) {
      switch (status) {
        case "提现":
          return "icontixianguanli";
        case "密码":
          return "iconanquanshezhi1";
        case "用款":
          return "iconyongkuanshenqing";
        case "审核":
          return "iconshenhe";
        case "回款":
          return "iconxiangmuhuikuan";
        default:
          return "iconquanbu";
      }
    },
    changeColor(status) {
      switch (status) {
        case "提现":
          return "backgroundColor:#FFB9BD";
        case "密码":
          return "backgroundColor:#FFBF81";
        case "用款":
          return "backgroundColor:#FFCF65";
        case "审核":
          return "backgroundColor:#FFC0A7";
        case "回款":
          return "backgroundColor:#FAD4F0";
        default:
          return "backgroundColor:#A7C5F5";
      }
    },
    changeStatus(status) {
      switch (status) {
        case 1:
          return false;
        case 0:
          return true;
      }
    },
    getList() {
      let obj = {
        pageNumber: this.paginationInfo.current,
        pageSize: this.paginationInfo.pageSize
      };
      this.$ajax
        .post(this.$api.getMsg, obj)
        .then(res => {
          this.$store.dispatch("changeReadNum", res.num);
          this.data = res.data;
          this.paginationInfo.total = res.total;
        })
        .catch(err => {
          this.$message.error(err);
        });
    }
  },
  created() {
    this.getList();
  },
  computed: {
    currentTabComponent() {
      if (this.$store.state.isPC) {
        return "pcWrapper";
      } else {
        return "contentWrapper";
      }
    },
    isPC() {
      return this.$store.state.isPC;
    }
  }
};
</script>

<style scoped lang="less">
.wrapper {
  padding: 0 1rem;
  .icon {
    display: inline-block;
    padding: 1.25rem;
    color: @white;
    border-radius: 50%;
  }
  .time {
    font-size: 1rem;
    font-family: MicrosoftYaHei;
    font-weight: 400;
    color: rgba(170, 170, 170, 1);
  }
  .avatar {
    position: relative;
    .dot-icon {
      position: absolute;
      right: 0;
      top: 0.7rem;
      -webkit-transform: translate(-50%, -50%);
      transform: translate(-50%, -50%);
      width: 0.7rem;
      height: 0.7rem;
      border-radius: 100%;
      background: #f5222d;
      z-index: 10;
      box-shadow: 0 0 0 1px #fff;
      display: inline-block;
    }
  }
}
</style>
